import safeStringify from './safeStringify';

export const strArgs=(args, mem)=>{
    var rst = [];
    var arg;
    if (!mem) {
        mem = [];
        //最大递归次数
        mem._atotal = 9999;
    }else{
        //最大递归次数
        if(mem._atotal--<=0){
            // console.log('递归次数：',mem._atotal);
            return rst;
        };
    }

    // console.log('递归次数：',mem._atotal);
    for (var i = 0; i < args.length; i++) {
        arg = args[i];
        //清理循环引用
        if (mem.indexOf(arg) > -1){
            continue;
        } else {
            // console.log(arg);
            mem.push(arg);
        }
        //避免遍历整个window对象
        if(arg===window||arg===document){
            continue;
        }

        if (arg instanceof Array) {
            rst = rst.concat(strArgs(arg));
            continue;
        }
        if (arg instanceof RegExp) {
            rst.push(x);
            continue;
        }
        if (arg instanceof Error) {
            try {
                rst.push(['err stack:', arg.message, arg.stack].join(''));
            } catch (ex) { }
            continue;
        }
        if (typeof arg === 'object') {
            for (var k in arg) {
                rst = rst.concat(['{', k, ':', strArgs([arg[k]], mem).join(','), '}'].join(''))
            }
            continue;
        }
        rst.push(arg);
    }
    return rst;
}

//通过webpack将本段代码注入到html中，或者直接复制
export const errorHandlerScript=function(){
    (function () {
        //请复制上方strArgs代码到这个地方
        function strArgs(args, mem) {
            var rst = [];
            var arg;
            if (!mem) {
                mem = [];
                //最大递归次数
                mem._atotal = 9999;
            }else{
                //最大递归次数
                if(mem._atotal--<=0){
                    // console.log('递归次数：',mem._atotal);
                    return rst;
                };
            }

            // console.log('递归次数：',mem._atotal);
            for (var i = 0; i < args.length; i++) {
                arg = args[i];
                //清理循环引用
                if (mem.indexOf(arg) > -1){
                    continue;
                } else {
                    // console.log(arg);
                    mem.push(arg);
                }
                //避免遍历整个window对象
                if(arg===window||arg===document){
                    continue;
                }

                if (arg instanceof Array) {
                    rst = rst.concat(strArgs(arg));
                    continue;
                }
                if (arg instanceof RegExp) {
                    rst.push(x);
                    continue;
                }
                if (arg instanceof Error) {
                    try {
                        rst.push(['err stack:', arg.message, arg.stack].join(''));
                    } catch (ex) { }
                    continue;
                }
                if (typeof arg === 'object') {
                    for (var k in arg) {
                        rst = rst.concat(['{', k, ':', strArgs([arg[k]], mem).join(','), '}'].join(''))
                    }
                    continue;
                }
                rst.push(arg);
            }
            return rst;
        }
        function alertErr(tag, _arguments, others) {
            tag = tag instanceof Array ? tag : [tag];
            var args = Array.prototype.slice.call(_arguments);
            args = tag.concat(args, others || []);
            var msg = strArgs(args).join('->');
            alert(msg);
            try {
                console.log(msg);
            } catch (ex) {
            }
        }
        window.onerror = function () {
            alertErr('windowErr', arguments);
        }
        window.onunhandledrejection = function (e) {
            alertErr(['promiseErr',e.reason,e],arguments);
        }
    })();
};
